package light.head.controller.el;

import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Url;
import light.head.dao.el.CourseDAO;
import light.head.dao.el.TopicAnswerDAO;
import light.head.dao.el.TopicCourseRelationDAO;
import light.head.dao.el.TopicDAO;
import light.head.dao.el.TopicDifficultyDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.utils.StringUtils;
import light.head.model.el.Course;
import light.head.model.el.Topic;
import light.head.model.el.TopicAnswer;
import light.head.model.el.TopicCourseRelation;
import light.head.model.el.TopicDifficulty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(Url.ADDDATA)
public class AddData extends SupporterController{
	private CourseDAO cDAO;
	private TopicDAO tDAO;
	private TopicDifficultyDAO lDAO;
	private TopicCourseRelationDAO tcrDAO;
	private TopicAnswerDAO tkDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.ADDDATA_INIT);
	}
	
	@RequestMapping(Url.SAVE)
	@Transactional(rollbackFor = Exception.class)
	public void save(Integer number ,HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		if(number != null && number > 0 ){
			Random rand = new Random();
			Date time = new Date();
			Integer oc = cu_organCode(request);
			Integer cu = cu_code(request);
			List<TopicDifficulty> lList = lDAO.getAll();
			Topic t = null;
			for(int i = 0 ; i < number ; i++){
				//添加题目
				t = new Topic();
				Integer type = rand.nextInt(3) + 1;	  // 题目类型
				Integer point = rand.nextInt(10) + 1; // 该题分数
				t.setPoint(point.floatValue());
				t.setType(type.byteValue());
				t.setContent(StringUtils.randStr(50));
				t.setTdc(lList.get(rand.nextInt(lList.size())).getCode());
				t.setCt(time);
				t.setDeleted(false);
				t.setCu(cu);
				t.setOc(oc);
				if(tDAO.add(t) == RESULT.SUCCESS){
					List<Course> cList = cDAO.findAllCourse();
					TopicCourseRelation tcr = null;
					Integer c = rand.nextInt(cList.size()) + 1;//所属课程数量
					/**
					 * 取到不重复的 课程ID 保存到数组中
					 */
					int[] cCodes = new int[c]; 
					int count = 0;  
				    while(count < c) {  
				        int num = (int) (Math.random() * (cList.size())) + 1;  
				        boolean flag = true;  
				        for (int j = 0; j < c; j++) {  
			        		if(num == cCodes[j]){  
				                flag = false;  
				                break;  
				            }  
				           
				        }  
				        if(flag){  
				        	cCodes[count] = num;  
				            count++;  
				        }  
				    }  
				    //添加 课程和题目关系
					for(int j = 0; j < cCodes.length ; j++){
						tcr = new TopicCourseRelation();
						tcr.setTc(t.getCode());
						tcr.setCc(cCodes[j]);
						if(tcrDAO.add(tcr) != RESULT.SUCCESS){
							throw new RuntimeException("课程添加失败");
						}
					}
					if(t.getType() == 1){
						TopicAnswer tk = null;
						int num = rand.nextInt(3) + 4;//答案数4~6
						int s = rand.nextInt(num);
						for(int j = 0; j < num; j++){
							tk = new TopicAnswer();
							tk.setAnswer(StringUtils.randStr(20));
							tk.setIsRight(j == s ? true : false);
							tk.setTc(t.getCode());
							tk.setCt(time);
							tk.setCu(cu);
							tk.setDeleted(false);
							tk.setOc(oc);
							if(tkDAO.add(tk) != RESULT.SUCCESS){
								throw new RuntimeException("答案添加失败(单选)");
							}
						}
					}else if(t.getType() == 2){
						TopicAnswer tk = null;
						int num = rand.nextInt(3) + 4;//答案数4~6
						int s = rand.nextInt(num) + 1;//正确答案数
						Boolean[] tts = new Boolean[num];
						/*for(Boolean tt : tts){
							tt = false;
						}*/
						for(int y = 0; y < s; y++){
							tts[rand.nextInt(num)] = true;
						}
						for(int j = 0; j < num; j++){
							tk = new TopicAnswer();
							tk.setAnswer(StringUtils.randStr(20));
							tk.setTc(t.getCode());
							tk.setCt(time);
							tk.setCu(cu);
							tk.setDeleted(false);
							tk.setOc(oc);
							tk.setIsRight(tts[j]);
							if(tkDAO.add(tk) != RESULT.SUCCESS){
								throw new RuntimeException("答案添加失败(多选)");
							}
						}
					}else if(t.getType() == 3){
						TopicAnswer tk = new TopicAnswer();
						tk = new TopicAnswer();
						tk.setAnswer(StringUtils.randStr(30));
						tk.setTc(t.getCode());
						tk.setCt(time);
						tk.setCu(cu);
						tk.setDeleted(false);
						tk.setOc(oc);
						if(tkDAO.add(tk) != RESULT.SUCCESS){
							throw new RuntimeException("答案添加失败(多选)");
						}
					}else{
						throw new RuntimeException("类型错误");
					}
					result = RESULT.SUCCESS;
				}
			}
		}
		if(RESULT.SUCCESS == result){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@Autowired
	public void setcDAO(CourseDAO cDAO) {
		this.cDAO = cDAO;
	}
	@Autowired
	public void settDAO(TopicDAO tDAO) {
		this.tDAO = tDAO;
	}
	@Autowired
	public void setlDAO(TopicDifficultyDAO lDAO) {
		this.lDAO = lDAO;
	}
	@Autowired
	public void setTcrDAO(TopicCourseRelationDAO tcrDAO) {
		this.tcrDAO = tcrDAO;
	}
	@Autowired
	public void setTkDAO(TopicAnswerDAO tkDAO) {
		this.tkDAO = tkDAO;
	}
}
